package com.cn;

import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class flowableTest02 {

    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Test
    void testFlowable02(){
        Deployment deploy = repositoryService.createDeployment()
                //获取数据路径
                .addClasspathResource("process01-基础/test02-测试方式.bpmn20.xml")
                //名称
                .name("流程变量模型")
                //部署方法吧
                .deploy();
        System.out.println("deploy.getId() ="+deploy.getId());
    }

    /**
     * 启动任务
     */
    @Test
    void testFlowable03(){
        String id = "test03:2:d9bc7c02-b04e-11ee-8050-30d04231d1ff";
        Map<String,Object> map = new HashMap<>();
        map.put("var1","test1");
        map.put("var2","test2");
        //启动成功时候的状态信息
        runtimeService.startProcessInstanceById(id,map);
    }

    /**
     * 流程启动设置信息
     */
    @Test
    void setAssinge(){
        String executionId = "fb427bca-b04e-11ee-bdd6-30d04231d1ff";
        runtimeService.setVariable(executionId,"var4","var5");
        runtimeService.setVariableLocal(executionId,"varLocal1","varLocal2");
        String taskId = "fb464c5e-b04e-11ee-bdd6-30d04231d1ff";
        taskService.setVariable(taskId,"task","task");
        taskService.setVariableLocal(taskId,"taskLocal","taskLocal");
    }

    /**
     * 获取变量信息
     */
    @Test
    void getAssinge(){
        String execId = "fb427bca-b04e-11ee-bdd6-30d04231d1ff";
        Map<String, Object> map = runtimeService.getVariables(execId);
        System.out.println(map);

        String taskId = "fb464c5e-b04e-11ee-bdd6-30d04231d1ff";
        System.out.println("task="+taskService.getVariables(taskId));
    }
    /**
     * 根据用户查询待办信息
     */
    @Test
    void testFlowable04(){
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("tiantian")
                .list();
        for (Task task : list) {
            System.out.println("task.getId() ="+task.getId());
        }
    }


    /**
     * 完成审批任务
     */
    @Test
    void testFlowable05(){
        //act_ru_task 中的任务id信息
        String id = "529032c5-b02e-11ee-a38d-30d04231d1ff";
        Map<String,Object> ver =new HashMap<>();
        ver.put("myAsssign1","tiantian");
//        taskService.complete(id,ver);
        taskService.complete(id);
//        String deleteId= "529032c5-b02e-11ee-a38d-30d04231d1ff";
//        taskService.deleteComment(deleteId);
    }

    /**
     * 设置用户设置为挂起状态
     */
    @Test
    void testFlowable056(){
        String processDeintionId = "";
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDeintionId)
                .singleResult();
        boolean suspended = processDefinition.isSuspended();
        if (suspended){
            System.out.println("激活流程");
            repositoryService.activateProcessDefinitionById(processDeintionId);
        }else {
            System.out.println("挂起流程");

        }
    }

}
